package com.pkcargo.web.cargo;

import com.pkcargo.entity.cargo.Cargo;
import com.pkcargo.entity.search.Metadata;
import com.pkcargo.service.ServiceException;
import com.pkcargo.service.search.SearchManager;
import com.pkcargo.service.cargo.CargoManager;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.web.struts2.CRUDActionSupport;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: Howard
 * Date: 2009-4-19
 * Time: 22:19:15
 * To change this template use File | Settings | File Templates.
 */
public class CargoAction extends CRUDActionSupport<Cargo> {
    @Autowired
    private CargoManager cargoManager;
    @Autowired
    private SearchManager searchManager;
    // 基本属性
    private Cargo entity;
    private Long id;

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    @Action(value = "/cargo/list",
            results = {@Result(name = "success", location = "/search?type=1", type = "redirect")}
    )
    public String list() throws Exception {
        return SUCCESS;
    }

    @Override
    @Action("/cargo/save")
    public String save() throws Exception {
        cargoManager.save(reload(entity));
        addActionMessage("保存货盘成功");
        return SUCCESS;

    }

    //辅助input，完善entity内容
    private Cargo reload(Cargo entity) throws Exception {
//        User user = new User();
//        Metadata md = new Metadata();
//        user.setId(userId);
//        md.setUser(user);
//        md.setPublishTime(new Date());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String allContent ="；起始地：" + entity.getStartCity()
                + "；目的地：" + entity.getEndCity()
                + "；货物类型" + entity.getCargoType()
                + "；有效时间：" + sdf.format(entity.getMetadata().getValidTime())
                + "；说明：" + entity.getComment()
                + "；联系方式：" + entity.getContact();
        Metadata md = entity.getMetadata();
        searchManager.save(md);
        md.setAllContent(allContent);
        md.setPublishTime(new Date());
        md.setType(1);//默认固定为1
        md.setValid(true);
        return entity;
    }

    @Override
    public String delete() throws Exception {
        try {
            cargoManager.delete(id);
            addActionMessage("删除货盘成功");
        } catch (ServiceException e) {
            logger.error(e.getMessage(), e);
            addActionMessage(e.getMessage());
        }
        return RELOAD;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = cargoManager.get(id);
        } else {
            entity = new Cargo();
        }
    }

    public Cargo getModel() {
        return entity;
    }
}
